/****************************************************************************/
/* File: scs-keyboard.cpp                                                   */
/* Desc: Implementation file for the input keyboard type                    */
/* Created: 2014-11-12                                                      */
/* Revised: 2014-11-13                                                      */
/* Authors: Sterling P. de Mille                                            */
/****************************************************************************/

// specification file
#include "scs-keyboard.h"

SCSkeyboard* SCSkeyboard::m_pkSingleton = NULL;

SCSkeyboard::SCSkeyboard ()
{
	Init();

	assert(!m_pkSingleton);
	m_pkSingleton = this;
}

SCSkeyboard::~SCSkeyboard ()
{
	if (m_piCurrentState != NULL) delete [] m_piCurrentState;
	if (m_piPreviousState != NULL) delete [] m_piPreviousState;

	m_piCurrentState = NULL;
	m_piPreviousState = NULL;

	assert(m_pkSingleton);
	m_pkSingleton = NULL;
}

SCSint SCSkeyboard::KeyCount ()
{
	return MAXKEYCODES;
}

void SCSkeyboard::Init ()
{
	m_piCurrentState = new SCSint8u[MAXKEYCODES];
	m_piPreviousState = new SCSint8u[MAXKEYCODES];

	assert(m_piCurrentState);
	assert(m_piPreviousState);

	memcpy(m_piCurrentState, SDL_GetKeyState(NULL), MAXKEYCODES);
	memcpy(m_piPreviousState, m_piCurrentState, MAXKEYCODES);
}

void SCSkeyboard::Update ()
{
	memcpy(m_piPreviousState, m_piCurrentState, MAXKEYCODES);
	memcpy(m_piCurrentState, SDL_GetKeyState(NULL), MAXKEYCODES);
}

SCSbool SCSkeyboard::KeyUp (SCSint iKeyCode)
{
	return !m_piCurrentState[iKeyCode];
}

SCSbool SCSkeyboard::KeyDown (SCSint iKeyCode)
{
	return m_piCurrentState[iKeyCode];
}

SCSbool SCSkeyboard::KeyPressed (SCSint iKeyCode)
{
	return (m_piCurrentState[iKeyCode] && !m_piPreviousState[iKeyCode]);
}

SCSbool SCSkeyboard::KeyReleased (SCSint iKeyCode)
{
	return (!m_piCurrentState[iKeyCode] && m_piPreviousState[iKeyCode]);
}

SCSkeyboard* SCSkeyboard::Singleton ()
{
	return m_pkSingleton;
}

